package com.hy.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.bean.Follow;
import com.hy.bean.PageBean;
import com.hy.dao.FollowDao;
import com.hy.dao.MessagesDao;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

public class FollowController {
//    private FollowDao followDao = new FollowDao();
    private FollowDao followDao = new FollowDao();
    private MessagesDao messagesDao = new MessagesDao();
    private ObjectMapper objectMapper =new ObjectMapper();

    public PageBean<Long> selectgz(HttpServletRequest request, HttpServletResponse response,Integer id){
        try {
            long count = followDao.selectgz(id);
            PageBean<Long> pb = new PageBean<>();

            pb.setCode(0001);
            pb.setMsg("ok");
            pb.setStatus(200);
            pb.setData(count);
            return pb;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public PageBean<List<Map<String, Object>>> selectavatar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int id = (int)session.getAttribute("id");

        PrintWriter out = response.getWriter();

        System.out.println(id);

        PageBean<List<Map<String, Object>>> pb = new PageBean<>();

        try {

           List<Map<String, Object>> maps = messagesDao.select(id);
            System.out.println(maps.toString());
            if (maps != null){
                pb.setMsg("数据传入成功");
                pb.setStatus(200);
                pb.setData(maps);
                pb.setCode(000100);
                //沙沟
//                String s = objectMapper.writeValueAsString(pb);
//
//                out.write(s);
//                out.flush();
//                out.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  pb;
    }

    public PageBean<Boolean> insertFol(HttpServletRequest request, HttpServletResponse response,Integer id,Integer uid) throws Exception {
        Follow follow = new Follow(id,uid,null,null,id,null,"0");
        long l = followDao.selectBy(id, uid);
        boolean flag = false;
        if(l == 0){
            flag = followDao.insert(follow);
        } else if (l > 0) {
            flag = followDao.update(follow);
        }

        PageBean<Boolean> pb = new PageBean();
        if (flag) {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(flag);
            return pb;
        } else {
            return null;
        }
    }

    public PageBean<Boolean> updateFol(HttpServletRequest request, HttpServletResponse response,Integer id,Integer uid) throws Exception {
        Follow follow = new Follow(id,uid,null,null,null,id,"1");
        boolean flag = followDao.update(follow);
        PageBean<Boolean> pb = new PageBean();
        if (flag) {
            pb.setCode(1);
            pb.setMsg("首页数据返回");
            pb.setStatus(500);
            pb.setData(flag);
            return pb;
        } else {
            return null;
        }
    }
}
